package com.weavernorth.hlymess;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.general.BaseBean;
import weaver.general.Util;

/**
 * @program: ecology_erdos
 * @description: 与汇联易对接获取token
 * @author: zm
 * @create: 2019-12-03 15:10
 **/


public class GetDealForhly extends BaseBean {
    public static Log log = LogFactory.getLog(GetDealForhly.class.getName());

    public String gainDealNum(String strUserid) {
        //基础类
        BaseBean bb = new BaseBean();
        //返回结果
        String strResult = "";
        //1.首先获取token
        String strTokenUrl = Util.null2String(bb.getPropValue("hlyMess", "tokenurl"));
        log.info("获取token的url" + strTokenUrl);
        String strDealUrl = Util.null2String(bb.getPropValue("hlyMess", "dealurl"));
        //请求接口获取token值
        try {
            String strTokenVal = CommonUtil.sendPost(strTokenUrl, "utf-8");
            //如果返回值不为空
            if (!"".equals(strTokenVal)) {
                //将返回值转为json
                JSONObject jsonToken = JSON.parseObject(strTokenVal);
                //获取返回值中的token值
                String strAccToken = Util.null2String(jsonToken.get("access_token"));
                //获取token_type
                String strTokenType = Util.null2String(jsonToken.get("token_type"));
                //首字母大写的token_type
                String strUpTokenType = "";
                //需将token_type首字母转为大写
                if (!"".equals(strTokenType)) {
                    strUpTokenType = strTokenType.substring(0, 1).toUpperCase() + strTokenType.substring(1, strTokenType.length());
                    log.info("转为大写的token_type值" + strUpTokenType);
                }

                //传输参数
                JSONObject jsonUser = new JSONObject();
                jsonUser.put("employeeId", strUserid);

                //根据token值和toekn_type获取待办数量
                strResult = CommonUtil.senDealPost(strDealUrl, jsonUser, strAccToken, strUpTokenType);
            }
        } catch (Exception e) {
            log.info("异常捕获" + e);
            e.printStackTrace();

        }


        return strResult;
    }


}
